<?php

require_once('Joob/Http/Request/Abstract.php');

/**
 * A simple Http Request incarnation
 * @author griever
 *
 */
class Joob_Http_Request_Http extends Joob_Http_Request_Abstract {

	public function __construct()	{
		parent::__construct();
		//push all request parameters
		$this->params = array_merge($this->params, $_REQUEST);
	}
	
	/**
	 * Get the specified server parameter
	 * @param unknown_type $key the parameter key
	 * @param unknown_type $default the default value
	 */
	public function getServer($key, $default = null)	{
		if (isset($_SERVER[$key]))	{
			return $_SERVER[$key];
		}
		return $default;
	}

	/**
	 * Get the request HTTP method
	 */
	public function getMethod()	{
		return $this->getServer("REQUEST_METHOD");
	}
	
	/**
	 * Test if the request is a POST request
	 */
	public function isPost()	{
		return ("POST" == $this->getMethod());
	}
	
	/**
	 * Test if the request is a GET request
	 */
	public function isGet()	{
		return ("GET" == $this->getMethod());
	}
	
	/**
	 * Get the specified request header
	 * @param unknown_type $header the header name
	 * @return the header value or false if we can't retrieve it
	 */
	public function getHeader($header)	{
		// Try to get it from the $_SERVER array first
        $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
        if (isset($_SERVER[$temp])) {
            return $_SERVER[$temp];
        }

        // This seems to be the only way to get the Authorization header on
        // Apache
        if (function_exists('apache_request_headers')) {
            $headers = apache_request_headers();
            if (isset($headers[$header])) {
                return $headers[$header];
            }
            $header = strtolower($header);
            foreach ($headers as $key => $value) {
                if (strtolower($key) == $header) {
                    return $value;
                }
            }
        }
        return false;
	}
	
	/**
	 * Test if this is a AJAX request
	 */
	public function isAjaxRequest()	{
        return ($this->getHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
	}
}

?>